package com.shensi.adminCollect.controller.integral;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.integral.CustomerPointsRecord;
import com.shensi.adminCollect.service.integral.ICustomerPointsRecordService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import com.shensi.adminCollect.service.system.ISysStationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 客户积分记录Controller111
 *
 * @author qujia
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/integral/record")
public class CustomerPointsRecordController extends BaseController
{
    @Autowired
    private ICustomerPointsRecordService customerPointsRecordService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysStationUserService stationUserService;
    /**
     * 查询客户积分记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerPointsRecord customerPointsRecord)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                customerPointsRecord.setStationIdList(list);
                customerPointsRecord.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            customerPointsRecord.setStationId(currentUser.getUser().getStationId());
        }
        startPage();
        List<CustomerPointsRecord> recordList = customerPointsRecordService.selectCustomerPointsRecordList(customerPointsRecord);
        return getDataTable(recordList);
    }

    /**
     * 查询客户积分记录列表
     */
    @GetMapping(path = {"/anonylist","/anony/list"})
    public TableDataInfo anonylist(CustomerPointsRecord customerPointsRecord)
    {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(customerPointsRecord.getStationId());
            if (list != null && list.size() > 0) {
                customerPointsRecord.setStationIdList(list);
                customerPointsRecord.setStationId(null);
            }
        startPage();
        List<CustomerPointsRecord> recordList = customerPointsRecordService.selectCustomerPointsRecordList(customerPointsRecord);
        return getDataTable(recordList);
    }

    /**
     * 导出客户积分记录列表
     */
    @GetMapping("/export")
    public AjaxResult export(CustomerPointsRecord customerPointsRecord)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                customerPointsRecord.setStationIdList(list);
                customerPointsRecord.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            customerPointsRecord.setStationId(currentUser.getUser().getStationId());
        }
        List<CustomerPointsRecord> recordList = customerPointsRecordService.selectCustomerPointsRecordList(customerPointsRecord);
        ExcelUtil<CustomerPointsRecord> util = new ExcelUtil<CustomerPointsRecord>(CustomerPointsRecord.class);
        return util.exportExcel(recordList, "record");
    }

    /**
     * 获取客户积分记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerPointsRecordService.selectCustomerPointsRecordById(id));
    }

    /**
     * 新增客户积分记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody CustomerPointsRecord customerPointsRecord)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPointsRecord.setStationId(currentUser.getUser().getStationId());
        customerPointsRecord.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsRecordService.insertCustomerPointsRecord(customerPointsRecord));
    }

    /**
     * 修改客户积分记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerPointsRecord customerPointsRecord)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPointsRecord.setStationId(currentUser.getUser().getStationId());
        customerPointsRecord.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsRecordService.updateCustomerPointsRecord(customerPointsRecord));
    }

    /**
     * 删除客户积分记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerPointsRecordService.deleteCustomerPointsRecordByIds(ids));
    }
}
